//
//  ImageViewController.m
//  FBSocialProject
//
//  Created by ZhangXiaofei on 2017/8/2.
//  Copyright © 2017年 YunJing. All rights reserved.
//

#import "FBCameraImageViewController.h"
#import "FBCameraViewUtils.h"
typedef void(^FBCameraImageHandler)(UIImage *image);

@interface FBCameraImageViewController ()
@property (strong, nonatomic) UIImage *image;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UILabel *infoLabel;
@property (strong, nonatomic) UIButton *cancelButton;
@property (nonatomic, copy) FBCameraImageHandler imageHandler;
@end

@implementation FBCameraImageViewController

- (instancetype)initWithImage:(UIImage *)image handler:(void (^)(UIImage *))handler {
    self = [super initWithNibName:nil bundle:nil];
    if(self) {
        _image = image;
        self.imageHandler = handler;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initialUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:animated];
    
}

- (void)initialUI {
    self.imageView.backgroundColor = [UIColor blackColor];
    
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height)];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.backgroundColor = [UIColor clearColor];
    self.imageView.image = self.image;
    [self.view addSubview:self.imageView];
    
    NSString *info = [NSString stringWithFormat:@"Size: %@  -  Orientation: %ld", NSStringFromCGSize(self.image.size), (long)self.image.imageOrientation];
    
    self.infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    self.infoLabel.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.7];
    self.infoLabel.textColor = [UIColor whiteColor];
    self.infoLabel.font = [UIFont fontWithName:@"AvenirNext-Regular" size:13];
    self.infoLabel.textAlignment = NSTextAlignmentCenter;
    self.infoLabel.text = info;
    //    [self.view addSubview:self.infoLabel];
    CGFloat W = 72;
    CGFloat H = W;
    CGFloat X = ([UIScreen mainScreen].bounds.size.width * 0.5 - W) * 0.5;

    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(X, self.view.frame.size.height - H - 48, W, H);
    [backBtn setImage:[UIImage imageNamed:@"FB_ChatCamera_cancel"] forState:UIControlStateNormal];
    backBtn.layer.cornerRadius = W * 0.5;
    backBtn.clipsToBounds = YES;
    [backBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(self.view.frame.size.width-X-W, self.view.frame.size.height - H - 48, W, H);
    [confirmBtn setImage:[UIImage imageNamed:@"FB_ChatCamera_confirm"] forState:UIControlStateNormal];
    confirmBtn.layer.cornerRadius = W * 0.5;
    confirmBtn.clipsToBounds = YES;
    [confirmBtn addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmBtn];
}

- (void)back:(UIButton *)btn {
    [self dismissViewControllerAnimated:NO completion:nil];

}

- (void)confirm:(UIButton *)btn {
    if (self.imageHandler) {
        self.imageHandler(_image);
    }
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];

}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    self.imageView.frame = self.view.contentBounds;
    
    [self.infoLabel sizeToFit];
    self.infoLabel.width = self.view.contentBounds.size.width;
    self.infoLabel.top = 0;
    self.infoLabel.left = 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
